package com.example.instantmessage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.instantmessage.adapter.ChatAdapter;
import com.example.instantmessage.adapter.MessageAdapter;
import com.example.instantmessage.fragment.MessageFragment;
import com.tencent.imsdk.v2.V2TIMAdvancedMsgListener;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMMessageManager;
import com.tencent.imsdk.v2.V2TIMMessageReceipt;
import com.tencent.imsdk.v2.V2TIMSendCallback;
import com.tencent.imsdk.v2.V2TIMSimpleMsgListener;
import com.tencent.imsdk.v2.V2TIMUserInfo;
import com.tencent.imsdk.v2.V2TIMValueCallback;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ChatActivity extends AppCompatActivity {
    private ListView msgListView;

    public static EditText inputText;

    private Button send;

    private ImageView arrow_left_bold_img;

    private TextView friends_id_tv;

    private ChatAdapter chatAdapter;

    private List<V2TIMMessage> msgList = new ArrayList<>();
    private List<V2TIMMessage> msgs = new ArrayList<>();


    private String friendsId;

    private static final String TAG = "ChatActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
//        //去除标题框
//        requestWindowFeature(Window.FEATURE_NO_TITLE);

        chatAdapter = new ChatAdapter(ChatActivity.this, R.layout.msg_item, msgList, friendsId);

        inputText = (EditText) findViewById(R.id.input_text);
        send = (Button) findViewById(R.id.send_btn);
        arrow_left_bold_img = findViewById(R.id.arrow_left_bold_img);
        friends_id_tv = findViewById(R.id.friends_id_tv);
        msgListView = (ListView) findViewById(R.id.msg_list_view);

        msgListView.setAdapter(chatAdapter);


        Intent intent = getIntent();
        friendsId = intent.getStringExtra("extra_data");
//        friendsId = "王五";
        friends_id_tv.setText(friendsId);

        initMsgs();


        arrow_left_bold_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(ChatActivity.this, MainActivity.class);
                startActivity(intent1);
            }
        });

        V2TIMManager.getMessageManager().addAdvancedMsgListener(new V2TIMAdvancedMsgListener() {
            @Override
            public void onRecvNewMessage(V2TIMMessage msg) {
                Log.e(TAG, "onRecvNewMessage: "+msg.getTextElem().getText() );
                msgList.add(msg);
                chatAdapter.notifyDataSetChanged();
            }

            @Override
            public void onRecvC2CReadReceipt(List<V2TIMMessageReceipt> receiptList) {
                super.onRecvC2CReadReceipt(receiptList);
            }

            @Override
            public void onRecvMessageRevoked(String msgID) {
                super.onRecvMessageRevoked(msgID);
            }

            @Override
            public void onRecvMessageModified(V2TIMMessage msg) {
                super.onRecvMessageModified(msg);
            }
        });

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = inputText.getText().toString();
//                String content = "你是小狗";
                if (!"".equals(content)) {

                    V2TIMManager.getInstance().sendC2CTextMessage(content, friendsId, new V2TIMSendCallback<V2TIMMessage>() {
                        @Override
                        public void onProgress(int progress) {

                        }

                        @Override
                        public void onSuccess(V2TIMMessage v2TIMMessage) {

                            msgList.add(v2TIMMessage);
                            Log.i(TAG, "onSuccess: " + content);
                            Log.e("====", Thread.currentThread().getName());
                            chatAdapter.notifyDataSetChanged();
                            inputText.setText("");
                        }

                        @Override
                        public void onError(int code, String desc) {
                            Log.i(TAG, "onError: 1111111111" + code + desc);

                        }

                    });


                }





            }


        });
    }

    private void initMsgs() {
        V2TIMManager.getMessageManager().getC2CHistoryMessageList(friendsId, 20, null, new V2TIMValueCallback<List<V2TIMMessage>>() {
            @Override
            public void onSuccess(List<V2TIMMessage> v2TIMMessages) {
                Log.i(TAG, "onSuccess: ");
                msgList.addAll(v2TIMMessages);


                //聊天列表倒序

                for (int i = msgList.size() - 1; i >= 0; i--) {
                    msgs.add(msgList.get(i));
                }
                msgList.clear();
                msgList.addAll(msgs);
//                Collections.reverse(msgList);

            }

            @Override
            public void onError(int code, String desc) {
                Log.i(TAG, "onError: ");
            }
        });
//        msgList = V2TIMMessageManager.getC2CHistoryMessageList(InstantMessageApplication.userId,20,null, new V2TIMValueCallback<List<V2TIMMessage>>() {
//            @Override
//            public void onSuccess(List<V2TIMMessage> v2TIMMessages) {
//                Log.i(TAG, "onSuccess: ");
//
//            }
//
//            @Override
//            public void onError(int code, String desc) {
//                Log.i(TAG, "onError: ");
//            }
//        });
    }


}